package com.example.easycharge.init;

import com.example.easycharge.entity.ChargePile;
import com.example.easycharge.error.QueueExistError;
import com.example.easycharge.repository.ChargePileRepository;
import com.example.easycharge.repository.VehicleRepository;
import com.example.easycharge.service.ChargeService;
import com.example.easycharge.service.PileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class QueueInit implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private VehicleRepository vehicleRepository;
    @Autowired
    private ChargePileRepository chargePileRepository;
    @Autowired
    private PileService pileService;
    @Autowired
    private ChargeService chargeService;


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        vehicleRepository.cleanVehicleState();
        chargePileRepository.cleanPileState();
        List<ChargePile> pileList = chargePileRepository.findAll();
        try {
            for (ChargePile chargePile : pileList) {
                pileService.addNewQueue(chargePile);
            }
        } catch (QueueExistError ignored) {

        }
        // TODO: 2022-03-29 在这里读取数据库内容而后初始化队列
    }
}
